package com.drcoffee.drcoffeemall.mapper;

import com.drcoffee.drcoffeemall.entry.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Mapper
@Repository
public interface UserMapper {

    /* 注册账号 */
    @Insert("INSERT INTO t_user(account, password, username,phone, email, firm_name, invoice_title, invoice_number, permission_id, invitation_code,erp_code) " +
            "VALUES (#{account}, #{password}, #{username},#{phone}, #{email}, #{firm_name}, #{invoice_title}, #{invoice_number},#{permission_id}, #{invitation_code},#{erp_code})")
    boolean register(User user);


    @Select("SELECT password FROM t_user where account = #{account} OR email = #{account} OR phone = #{account}")
    String getUserPass(String account);

    @Select("SELECT a.*, CONCAT(b.o_file_path, b.folder, a.photo_url) as file_path " +
            "FROM t_user a, t_file_path b " +
            "WHERE (a.account = #{account} or a.email = #{account} or phone = #{account}) " +
            "AND b.bsh_one = 'H' " +
            "AND b.bsh_two = 1")
    User accountLogin(User user);

    /* 根据账号查找用户 */
    @Select("SELECT a.*, CONCAT(b.o_file_path, b.folder, a.photo_url) as file_path " +
            "FROM t_user a,t_file_path b " +
            "WHERE account = #{account} AND b.bsh_one = 'H' AND b.bsh_two = 1")
    User getUserByAccount(String account);

    /* 校验账号是否已被注册 */
    @Select("SELECT count(account) FROM t_user WHERE account = #{account}")
    Integer verifyAccount(String account);

    /* 设置默认地址 */
    @Update("UPDATE t_user SET default_address= #{address_id} WHERE account = #{account}")
    void setDefaultAddress(@Param("address_id") Integer address_id, @Param("account") String account);

    /* 查询头像上传路径 */
    @Select("SELECT CONCAT(i_file_path, folder) AS input_path FROM t_file_path WHERE bsh_one = 'H' AND bsh_two = 1")
    String getPhotoInputPath();

    /* 修改用户信息 */
    boolean uploadUser(User user);

    @Update("UPDATE t_user SET photo_url = #{photo_url} WHERE id = #{id}")
    boolean uploadUserPhoto(Integer id, String photo_url);

    /* 查询用户类型 */
    @Select("SELECT permission_id FROM t_user WHERE account = #{account}")
    Integer getUserType(String account);


    /* 根据邮箱或手机号查询用户 */
    @Select("SELECT * FROM t_user WHERE phone = #{account} OR email = #{account}")
    User getUserByCE(String account);

    /* 根据邮箱或手机号查询用户数量 */
    @Select("SELECT count(*) FROM t_user WHERE phone = #{val} OR email = #{val}")
    Integer isExist(String val);

    /* 修改密码（根据邮箱找回密码） */
    @Update(" UPDATE t_user SET password = #{password} WHERE email = #{email}")
    boolean retrievePass(String email, String password);

    @Select("SELECT a.*, CONCAT(b.o_file_path, b.folder, a.photo_url) as file_path " +
            "FROM t_user a, t_file_path b  " +
            "WHERE ${splice} = #{key}  AND b.bsh_one = 'H' AND b.bsh_two = 1")
    User getUserByPhoneOrEmail(String splice, String key);

    /* 原密码校验 */
    @Select("SELECT count(account) FROM t_user WHERE account = #{account} AND password = #{oldPass}")
    Integer verifyPass(String account, String oldPass);

    /* 更改密码 */
    @Update("UPDATE t_user SET password = #{newPass} WHERE account = #{account}")
    boolean changePass(String account, String newPass);

    /* 更改邮箱 */
    @Update("UPDATE t_user SET email = #{email} WHERE account = #{account}")
    boolean changeEmail(String account, String email);

    /* 更改名称 */
    @Update("UPDATE t_user SET username = #{newName} WHERE account = #{account}")
    boolean changeName(String account, String newName);

    @Select("SELECT CONCAT(i_file_path, folder) as i_file_path FROM t_file_path WHERE bsh_one = 'H' AND bsh_two = 1")
    String getAvatarPath();

    @Update("UPDATE t_user SET account = #{account},password = #{password},username = #{username},signin_date=NOW() WHERE email = #{email}")
    void setAccount(User user);

    @Select("SELECT * FROM t_invitation_code WHERE code = #{code} AND status = 'N'")
    InvitationCode invitationCheck(Integer code);

    @Select("SELECT default_address FROM t_user WHERE account = #{account}")
    Integer getDefaultAddressID(String account);

    @Update("UPDATE t_user SET phone = #{phone} WHERE account = #{account}")
    void changePhone(String account, String phone);

    @Insert("INSERT INTO t_feedback (sketch,details,image_url,user_account) VALUES (#{sketch},#{details},#{image_url},#{user_account})")
    void putFeedback(Feedback feedback);

    @Update("UPDATE t_user SET password = #{password} WHERE account = #{account}")
    void setPass(User user);

    @Select("SELECT COUNT(0) FROM t_firm_alter WHERE user_account = #{account} AND status = 0")
    Integer getAlterFirmPD(String account);

    @Insert("INSERT INTO t_firm_alter(user_account, firm_name, invoice_title, invoice_number, apply_for_date) VALUES(#{user_account}, #{firm_name}, #{invoice_title}, #{invoice_number}, #{apply_for_date})")
    void addFirmAlter(FirmAlter firmAlter);

    @Update("UPDATE t_invitation_code SET status = 'Y' WHERE code = #{code}")
    void setInvitationCheckStatus(String status, String code);

    @Insert("INSERT INTO t_exception(date,path,ip,address,private_ip) VALUES (#{date},#{path},#{ip},#{address},#{private_ip})")
    void setExceptionRecord(ExceptionRecord exceptionRecord);

    @Select("SELECT COUNT(0) FROM t_user WHERE email = #{email} AND account IS NOT NULL")
    Integer haveRegistered(String email);
}
